package com.dmall.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class SqliteJDBCTest {

	@Test
	public void test1() {
		String driver = "org.sqlite.JDBC";
		String url = "jdbc:sqlite:C:/tmp/sqlite/test.db";
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url);
			String sql = "insert into test(pro_id, name, birth, gender, introduction) values(?,?,?,?,?)";
			ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			ps.setLong(1, 201810140000225L);
			ps.setString(2, "lisi");
			ps.setString(3, "2018-10-16 12:00:00");
			ps.setInt(4, 0);
			ps.setString(5, "hehe");
			int count = ps.executeUpdate();
			System.out.println("count = " + count);
			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next()) {
				System.out.println("主键：" + rs.getInt(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				ps = null;
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				conn = null;
			}
		}
	}
}
